<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/4/11 11:52
 * Remark:
 */

namespace App\Models\Approval;

use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;

class OldDingTalkApprovalModel extends BaseModel
{
    protected $table  = TableConsts::CY_DING_TALK_APPROVAL_TABLE_NAME;

    protected  $primaryKey = TableConsts::CY_DING_TALK_APPROVAL_TABLE_PRIMARY_KEY;

    protected $fillable = [
        'ding_talk_approval_id',
        'originator_staff_id',
        'ding_talk_approval_process_id',
        'ding_talk_approval_related_personnel_staff_ids',
        'ding_talk_approval_type_id',
        'form_component',
        'create_time',
        'finish_time',
        'operation_records',
    ];


    public function editApproval($processId,$data){
        return $this->where('ding_talk_approval_process_id',$processId)->update($data);
    }

    public function isExistByCondition($conditionData){
        return $this
            ->withoutGlobalScopes()
            ->firstOrnew($conditionData)->exists;
    }

    /**
     * 新增审批
     * @param $talkApprovalData
     * @return mixed
     */
    public function addTalkApproval($talkApprovalData): mixed
    {
        return $this->insertGetId($talkApprovalData);
    }

    public function getTalkApprovalInfoByTableId($processId,$select = ["*"]){
        return $this
            ->where("ding_talk_approval_id",$processId)
            ->select($select)
            ->first();
    }

    public function getTalkApprovalInfoByDingTalkApprovalProcessId($processId,$select = ["*"]){
        return $this
            ->where("ding_talk_approval_process_id",$processId)
            ->select($select)
            ->first();
    }


    public function getTalkApprovalLogBySourceIdSourceType($sourceId,$sourceType){
        return $this
            ->select([
                'form_component',
                'ding_talk_approval_process_id',
                'finish_time',
                'operation_records'
            ])
            ->where('submit_approval_source_id',$sourceId)
            ->where('submit_approval_type',$sourceType)
            ->orderBy('ding_talk_approval_id','desc')
            ->first();
    }

    public function getTalkApprovalLogBySourceIdSourceTypeArr($sourceId,$sourceType)
    {
        return $this
            ->select([
                'ding_talk_approval_id',
                'ding_talk_approval_process_id',
            ])
            ->where('submit_approval_source_id',$sourceId)
            ->where('submit_approval_type',$sourceType)
            ->where('ding_talk_approval_type_id',"!=",4)
            ->get();
    }

}